import initDatabase from './app/database/test-setup';
import GroupScore from './app/service/core/group-score';
import Email from './app/util/email';

const { NODE_ENV } = process.env;

class AppBootHook {
  app: any;

  constructor(app) {
    this.app = app;
  }

  async willReady() {
    this.app.passport.verify(async (ctx, manager) => {
      if (manager.payload.uuid) {
        const loginManager = await ctx
          .service
          .manager
          .findManagerByUuid(manager.payload.uuid);

        if (loginManager) return loginManager;
      }
      return manager.payload.uuid;
    });
  }

  // eslint-disable-next-line class-methods-use-this
  async didLoad() {
    if (NODE_ENV === 'test') {
      await initDatabase();
    }
  }

  async serverDidReady() {
    // 初始化 GroupScore 的 static 属性 conclusionList
    const ctx = await this.app.createAnonymousContext();
    const conclusionList = await ctx.service.conclusion.searchQuestionListByConclusion();
    GroupScore.initConclusionList(conclusionList);

    // email 实例初始化
    Email.init();
  }
}
module.exports = AppBootHook;
